package com.uton.rfidcontroller.view.activity.write.prescan;

import android.content.Intent;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.common.rfid.UfhData;
import com.uton.rfidcontroller.common.utils.LogUtil;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.databinding.ActivityFinalWriteBinding;
import com.uton.rfidcontroller.view.activity.SettingActivity;
import com.uton.rfidcontroller.view.base.BaseActivity;
import com.uton.rfidcontroller.viewmodel.activity_model.FinalWriteActivityViewModel;

/**
 * Created by 王家宝 on 2017/10/16.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class FinalWriteActivity extends BaseActivity<ActivityFinalWriteBinding, FinalWriteActivityViewModel> {

    public static final int STANDARD_LENGTH = 24;
    public static final int WRITE_SUCCESS = 0;
    public static final int WRITE_FAILED = -1;
    private boolean isWriteInSuccess;

    @Override
    protected int setLayout() {
        return R.layout.activity_final_write;
    }

    @Override
    protected void inject() {
        getBinding().setModel(getViewModel());
    }

    @Override
    protected String setTitle() {
        return getString(R.string.ui_input);
    }

    @Override
    protected void doSomething() {
        getViewModel().cardId.set(UfhData.getUfh_id());
    }

    @Override
    protected void doOnFinish() {
        super.doOnFinish();
        Intent intent = new Intent();
        intent.putExtra(GlobalConstants.IS_WRITE_IN_SUCCESS, isWriteInSuccess);
        setResult(GlobalConstants.RESULT_IS_WRITE_SUCCESS, intent);
    }

    @Override
    protected FinalWriteActivityViewModel setViewModel() {
        return new FinalWriteActivityViewModel(
                getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1)) {
            @Override
            public void writeIn() {
                // TODO: 2017/10/16 最终写入RFID卡片
                //判断是否开启扫描
                if (!UfhData.isDeviceOpen()) {
                    Utils.showShortToast(getString(R.string.toast_not_open_device));
                    startActivity(new IntentBuilder().from(FinalWriteActivity.this, SettingActivity.class).getIntent());
                    return;
                }
                String rfidCardId = getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1);
                if (rfidCardId != null && rfidCardId.length() != STANDARD_LENGTH) {
                    return;
                }

                int result = UfhData.UhfGetData.Write6c(
                        (byte) 6, (byte) (UfhData.epcBytes.get(UfhData.getUfh_id()).length / 2),
                        UfhData.epcBytes.get(UfhData.getUfh_id()), (byte) 1, (byte) 2,
                        UfhData.UhfGetData.hexStringToBytes(rfidCardId),
                        UfhData.UhfGetData.hexStringToBytes("00000000"));

                LogUtil.d("写入的结果是：" + result);

                String resultMsg;
                if (result == WRITE_SUCCESS) {
                    resultMsg = "写入成功";
                    isWriteInSuccess = true;
                } else if (result == WRITE_FAILED) {
                    resultMsg = "写入失败";
                    isWriteInSuccess = false;
                } else {
                    resultMsg = "未知错误";
                    isWriteInSuccess = false;
                }

                getViewModel().writeInState.set(resultMsg);

            }
        };
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(GlobalConstants.IS_WRITE_IN_SUCCESS, isWriteInSuccess);
        setResult(GlobalConstants.RESULT_IS_WRITE_SUCCESS, intent);
        finish();
    }
}
